﻿using EquipmentManagement.Application.IServices;
using EquipmentManagement.Domain.Entities;
using Infrastructure;
using Standard;
using Standard.Dtos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Schema;
using Yitter.IdGenerator;

namespace EquipmentManagement.Application.Services
{
    public class DeviceTypeService : IDeviceTypeService
    {
        private readonly MesRepository<DeviceType> _devicetype;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="devicetype"></param>
        public DeviceTypeService(MesRepository<DeviceType> devicetype)
        {
            _devicetype = devicetype;
        }
        /// <summary>
        /// 添加具体实现
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ApiResult<int> Add(DeviceType model)
        {
            try
            {
                model.ID = YitIdHelper.NextId().ToString();
                model.CreatedTime = DateTime.Now;
                model.IsDeleted = false;
                model.CreatedUserName = "admin";
                var i = _devicetype.Add(model);
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = i,
                    Message = "添加成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除具体实现
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<int> Delete(string id)
        {
            try
            {
                var i = id.Split(",");
                foreach (var item in i)
                {
                    var model = _devicetype.GetEntity(x => x.ID == item);
                    model.IsDeleted = true;
                    _devicetype.Update(model);
                }

                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = 1,
                    Message = "删除成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取具体实现
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ApiResult<DeviceType> Get(string id)
        {
            try
            {
                return new ApiResult<DeviceType>
                {
                    Code = ApiCode.Success,
                    ResultData = _devicetype.GetEntity(x => x.ID == id),
                    Message = "成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取列表具体实现（分页）
        /// </summary>
        /// <param name="paging"></param>
        /// <returns></returns>
        public ApiPaging<DeviceType> GetList(PagingInput paging)
        {
            try
            {
                var model = _devicetype.GetQueryable(x => x.IsDeleted == false);
                return new ApiPaging<DeviceType>
                {
                    TotalCount = model.Count(),
                    TotalPage = (int)Math.Ceiling(model.Count() * 1.0 / paging.PageSize),
                    PageDatas = model.OrderByDescending(x => x.ID).Skip((paging.PageIndex - 1) * paging.PageSize).Take(paging.PageSize).ToList(),
                    Code = ApiCode.Success,
                    Message = "成功"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取列表具体实现
        /// </summary>
        /// <returns></returns>
        public ApiResult<List<DeviceType>> Gets()
        {
            return new ApiResult<List<DeviceType>>
            {
                Code = ApiCode.Success,
                ResultData = _devicetype.GetQueryable(x=>!x.IsDeleted).ToList(),
                Message = "成功"
            };
        }

        /// <summary>
        /// 修改具体实现
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ApiResult<int> Update(DeviceType model)
        {
            try
            {
                model.UpdateTime = DateTime.Now;
                model.UpdateUserName = "admin";
                return new ApiResult<int>
                {
                    Code = ApiCode.Success,
                    ResultData = _devicetype.Update(model),
                    Message = "修改成功"
                };
            }
            catch (Exception ex)
            {
                throw new Exception("修改设备类型异常");
            }
           
        }



    }
}
